import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.sql.rowset.serial.SerialBlob;

public class DBManager
{	
	// db infomation
	private final String url = "jdbc:oracle:thin:@155.230.193.42:1521:oraknu";
	private final String user = "system";
	private final String pass = "DBLab012345";
	
	private Connection connection = null;	
	private static DBManager manager = null;
	
	
	private DBManager()
	{
		connect();		
	}
		
	public static DBManager getInstance()
	{
		if(manager == null) 
			return (manager = new DBManager());
		else
			return manager;
	}
	
	public void  connect()
	{
		try
		{
			Class.forName("oracle.jdbc.driver.OracleDriver");
		}
		catch(ClassNotFoundException e)
		{
			System.err.println("error = " + e.getMessage());
			System.exit(1);
		}
		
		try
		{
			connection = DriverManager.getConnection(url, user, pass);
			connection.setAutoCommit(false);
		}
		catch(SQLException e)
		{
			System.err.println("sql error = " + e.getMessage());
			System.exit(1);
		}
	}
	
	public boolean insertArticle(Article _article)
	{
		try
		{
			String query = "INSERT INTO ARTICLE"
					+ "(AID, PUBDATE, CONTENTS, TITLE, THUMB, ID_C)"
					+ "VALUES (?, ?, ?, ?, ?, ?)";
			
			PreparedStatement  pstmt = connection.prepareStatement(query);
			
			pstmt.setInt(1, _article.getAarticleId());
			pstmt.setDate(2, new java.sql.Date(_article.getPublishTime().getTime()));
			pstmt.setString(3, _article.getArticleContents());
			pstmt.setString(4, _article.getArticleTitle());
			pstmt.setString(5, _article.getArticleThumb());
			pstmt.setInt(6, _article.getCategoryId());
			pstmt.executeUpdate();
			connection.commit();
			pstmt.close();

		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			//e.printStackTrace();
			//System.out.println("Err categori :" + _article.getCategoryId() +  ", id : " + _article.getAarticleId());
			return false;
		}		
		
		return true;
	}
}
